home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / Polygondrian 1.0.2 / Polygondrian.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  6.3 KB  |  252 lines  |  [TEXT/CWIE]

  1. // Polygondrian
  2. // version 1.0.2
  3. // by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 961201
  5.  
  6. //• This is a little color salad tossed together by Kenneth A. Long (me).
  7. //• I wanted to do some polygon action and this seemed like a good way to
  8. //• do it.  Random poly's sure do be fast!  Huh?    :-)
  9.  
  10. //• Enjoy!
  11.  
  12. //• kenlong@netcom.com        10 March 1994.
  13. //•-----------------------------------------------------------------------•//
  14.  
  15. //•  Polygondrian.c •//
  16.  
  17. //• Constance •//
  18. //• Ain't got none.
  19.  
  20. //• Globules •//
  21. Rect            windRect;
  22. OSErr            error;
  23. SysEnvRec        theWorld;
  24. CGrafPtr        savePort;
  25. Rect            r;
  26. PolyHandle        poly;
  27. Pattern            p;
  28. WindowPtr        window;
  29. short            horiz, vert;
  30. Boolean            QDAvail;
  31. RGBColor         rgb;
  32. Boolean            hasColorQD;
  33.  
  34. RgnHandle        mBarRgn, GrayRgn;
  35. short            *mBarHeightPtr;
  36. short            twenty_pixels;
  37.  
  38. //• Prototypes •//
  39. void            Random_Poly_Points (Rect *rectPtr);
  40. unsigned short    The_Ramdominator (unsigned short min, unsigned short max);
  41. void            Draw_The_Poly (void);
  42. void            Do_Init_Managers (void);
  43. void            Hide_Menu_Bar (void);
  44. void            Show_Menu_Bar (void);
  45. Boolean            Check_The_Equipment (void);
  46. void            Set_Up_Window (void);
  47. void            main (void);
  48. int                Main_Event_Loop (void);
  49.  
  50. void Random_Poly_Points (Rect *rectPtr)
  51. {
  52.     unsigned short start_H, start_V;
  53.  
  54.     window = FrontWindow ();
  55.     
  56.     //• Start recording a polygon.
  57.     poly = OpenPoly ();
  58.     
  59.     //• Pick a random hoizontal and vertical point for our starting point.
  60.     start_H = The_Ramdominator (0, qd.screenBits.bounds.right);
  61.     start_V = The_Ramdominator (0, qd.screenBits.bounds.bottom);
  62.     
  63.     //• Move the pen to that location.
  64.     MoveTo (start_H, start_V);
  65.  
  66.     //• Set up 5 random poly bounds lines starting from there.
  67.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  68.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  69.             
  70.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  71.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  72.             
  73.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  74.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  75.             
  76.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  77.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  78.             
  79.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  80.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  81.     
  82.     //• You could have as many successive LineTo's here as you wanted,
  83.     //• within reason.  Also, you could put specific values in, or even get
  84.     //• a control value.  You could get a previous value and add to 
  85.     //• them or subtract from them for a series effect (like ZoomIdle).
  86.     
  87.     //• Finish the polygon by drawing a line back to the starting point.
  88.     LineTo (start_H, start_V);
  89.             
  90.     //• Stop recording.
  91.     ClosePoly ();
  92. }
  93.  
  94. unsigned short The_Ramdominator (unsigned short min, unsigned short max)
  95. {
  96.     unsigned long ranger, tonto;    //• I knew that one would throw ya!
  97.     unsigned short el_randomo;        //• No sign means pos. and neg. values.
  98.     
  99.     //• Get a random number (based on the date and the time at that call).
  100.     el_randomo = Random ();
  101.     
  102.     //• Just to be sure...if it's less than 0 (which it shouldn't be)
  103.     //• then multiply by -1 to make it positive.
  104.     if (el_randomo < 0)
  105.         el_randomo *= -1;
  106.     
  107.     //• The next three lines are a formula for generating a random
  108.     //• number within a specific range.  Don't worry about the 
  109.     //• mathematics, just know where you can look such a formula up 
  110.     //• if you ever need it.
  111.     
  112.     //• The range IS the maximum value minus the minimum value.
  113.     ranger = max - min;
  114.     
  115.     //• These two bytes hold the random number, muliplied by the range
  116.     //• then that product divided by 65,535.
  117.     tonto = (el_randomo * ranger) / 65535;
  118.     
  119.     //• Then, that product is added to minimum value and returned to caller.
  120.     return (tonto + min);   
  121. }
  122.  
  123. //• Set the forecolor with a single line.
  124. SetForeColor (short red, short green,short  blue)
  125. {
  126.     RGBColor theColor;
  127.  
  128.     theColor.red = red;
  129.     theColor.green = green;
  130.     theColor.blue = blue;
  131.     RGBForeColor(&theColor);
  132. }
  133.  
  134. //• Initialize everything for the program, make sure we can run.
  135. void Draw_The_Poly (void)
  136. {
  137.     Rect         poly_wrecked;
  138.     RGBColor    poly_color;
  139.     
  140.     poly_color.red   = Random ();        //• So are there 281,474,976,710,656
  141.     poly_color.green = Random ();        //• possible combinations?
  142.     poly_color.blue  = Random ();        //• Try 2 /*814749767106*/ 56 :)
  143.     
  144.     Random_Poly_Points (&poly_wrecked);    //• Figure the poly points.
  145.     
  146.     RGBForeColor (&poly_color);            //• Snag a random color for the RGB.
  147.     
  148.     //• Color paint it.
  149.     PaintPoly (poly);
  150.  
  151.     //• Say what color the line is.
  152.     SetForeColor (0, 0, 0);
  153.     
  154.     //• Suitable for framing!
  155.     FramePoly (poly);            //• They're uglier with no frame!
  156.     
  157.     //• Bury it.
  158.     KillPoly (poly);
  159.  
  160. }    //• Now, "doitallagainreallyreallyfast!!!" (while not button, remember?).
  161.  
  162. void Do_Init_Managers (void)
  163. {
  164.     MaxApplZone ();
  165.  
  166.     InitGraf (&qd.thePort);
  167.     InitFonts ();
  168.     InitWindows ();
  169.     InitMenus ();
  170.     TEInit ();
  171.     InitDialogs (nil);
  172.     InitCursor ();
  173. }
  174.  
  175. void Hide_Menu_Bar (void) 
  176. {
  177.     Rect    mBarRect;
  178.  
  179.     GrayRgn = GetGrayRgn ();
  180.     mBarHeightPtr = (short *)  0x0BAA;
  181.     twenty_pixels = *mBarHeightPtr;
  182.     *mBarHeightPtr = 0;
  183.     mBarRect = qd.screenBits.bounds;
  184.     mBarRect.bottom = mBarRect.top + twenty_pixels;
  185.     mBarRgn = NewRgn ();
  186.     RectRgn (mBarRgn, &mBarRect);
  187.     UnionRgn (GrayRgn, mBarRgn, GrayRgn);
  188.     PaintOne (0L, mBarRgn);
  189. }
  190.  
  191. void Show_Menu_Bar (void) 
  192. {
  193.     *mBarHeightPtr = twenty_pixels;
  194.     DiffRgn (GrayRgn, mBarRgn, GrayRgn);
  195.     DisposeRgn (mBarRgn);
  196. }
  197.  
  198. Boolean Check_The_Equipment ()
  199. {
  200.     SysEnvRec mySE;
  201.     
  202.     SysEnvirons (2,&mySE);
  203.     
  204.     if (!mySE.hasColorQD)
  205.         ExitToShell ();
  206. }
  207.  
  208. void Set_Up_Window (void)
  209. {
  210.     windRect = qd.screenBits.bounds;
  211.     
  212.     window = NewCWindow (0L, &windRect, "\p", true, plainDBox, (WindowPtr)-1L, false, 0);
  213.     if (window == nil)
  214.     {
  215.         SysBeep (10);
  216.         ExitToShell ();
  217.     }
  218.     //• Say where we'll draw.  Set window to current graf port.
  219.     SetPort (window);
  220.  
  221.     //• Background color.
  222.     FillRect (& (window->portRect), &qd.black);    //• We already know it's portRect!
  223. }
  224.  
  225. void main (void)
  226. {
  227.     long ticks;
  228.  
  229.     Do_Init_Managers ();
  230.  
  231.     Check_The_Equipment ();
  232.     Hide_Menu_Bar ();
  233.     Set_Up_Window ();
  234.  
  235.     //• The seed for the random number generation.
  236.     GetDateTime ( (unsigned long*) &qd.randSeed);
  237.  
  238.     HideCursor ();                //• Don't want that 'fly' buzzin' around.
  239.  
  240.     while (! Button ())            //• While we ain't clickin...
  241.     {
  242.         Draw_The_Poly ();        //• draw to the tickin'.
  243. //•        Delay (60L, &ticks);    //• Put the brakes on, a little...
  244.                                 //• (uncomment for slow).
  245. //•     FillRect (& (window->portRect), black);
  246.                                 //• (uncomment for single).
  247.     }
  248.         ShowCursor ();
  249.     Show_Menu_Bar ();
  250.     ExitToShell ();
  251. }
  252.